package com.xiaouyudeguang.common.aspect;

import com.xiaouyudeguang.common.utils.DateUtils;
import com.xiaouyudeguang.common.utils.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class LogAspect extends AopAspect {

    /**
     * 打印参数日志
     *
     * @param pjp  切面
     * @param url  url
     * @param todo 描述
     * @return
     * @throws Throwable
     */
    protected Object doPrint(ProceedingJoinPoint pjp, String url, String todo) throws Throwable {
        todo = StringUtils.isEmpty(todo) ? realMethod.getName() : todo;
        String description = StringUtils.defaultOfEmpty(todo, realClass.getName() + "." + realMethod.getName());
        Logger logger = LoggerFactory.getLogger(pjp.getTarget().getClass());
        logger.debug(new StringBuilder("【").append(requestId).append("】")
                .append("\n=========================请求开始处理=========================")
                .append("\n开始时间 =>  ").append(DateUtils.getNowMs())
                .append("\n请求描述 =>  ").append(description)
                .append("\n请求路径 =>  ").append(url)
                .append("\n请求方法 =>  ").append(realMethod.getName())
                .append("\n请求执行 =>  ")
                .toString());
        Object res = doProceed(pjp);
        logger.debug(new StringBuilder("【").append(requestId).append("】")
//                .append("\n请求结果 =>  ").append(JsonObject.toJson(res))
                .append("\n结束时间 =>  ").append(DateUtils.getNowMs())
                .append("\n消耗时间 =>  ").append((System.currentTimeMillis() - startTime) + "ms")
                .append("\n=========================请求结束处理=========================\n")
                .toString());
        return res;
    }

    /**
     * 调用业务逻辑
     *
     * @param pjp
     * @return
     * @throws Throwable
     */
    protected Object doProceed(ProceedingJoinPoint pjp) throws Throwable {
        return pjp.proceed();
    }
}
